package com.workManager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.mapper.TdOrganizationMapper;
import com.workManager.mapper.TdRoleMapper;
import com.workManager.mapper.TdUserMapper;
import com.workManager.mapper.TdUserRoleMapper;
import com.workManager.pojo.*;
import com.workManager.util.SecurityUtil;
import com.workManager.vo.UserVo;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Thinkpad on 2017/1/12 0012.
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private TdUserMapper userMapper;
    @Autowired
    private TdUserRoleMapper userRoleMapper;
    @Autowired
    private TdOrganizationMapper organizationMapper;

    @Autowired
    private TdRoleMapper roleMapper;

    public void register(TdUser tdUser) {
        userMapper.insert(tdUser);
    }

    public void login(TdUser tdUser) {

    }

    public void loginOut() {

    }

    public List<TdUser> queryUser() {
     return    userMapper.selectByExample(null);
    }

    public TdUser queryUserByLoginCode(String loginCode) {
            return   userMapper.queryUserByLoginCode(loginCode);

    }

    public TdUser queryUserByName(String name) {
        return null;
    }

    public void passwordModify(String loginCode, String passWord) {
        Map record = new HashMap();
        record.put("loginCode",loginCode);
        record.put("passWord",passWord);
        userMapper.passwordModify(record);
    }

    public PageInfo<TdUser> queryByOraganizationId(Map<String, Object> map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);

        List<TdUser> userList = userMapper.queryByOraganizationId(map);
        PageInfo<TdUser> pageInfo = new PageInfo<TdUser>(userList);

        return pageInfo;
    }

    public Map<String,Object> query(Map<String,Object> map) throws Exception{
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);

        Map<String,Object> dataMap = new HashMap<String,Object>();

        List<UserVo> userVoList = userMapper.query(map);
        PageInfo<UserVo> userVoPageInfo = new PageInfo<UserVo>(userVoList);
       /* List<TdUser> userList = userMapper.query(map);
        List<UserVo> userVoList=new ArrayList<UserVo>();
        if (userList!=null){
            for (TdUser user:userList){
                UserVo userVo=new UserVo();
                PropertyUtils.copyProperties(userVo,user);
                userVo.setPassword(null);
                //todo set orgName roleName
                if (userVo.getOrganizationId()!=null){
                    TdOrganization organization=organizationMapper.selectByPrimaryKey(userVo.getOrganizationId());
                    if (organization!=null)
                        userVo.setOrgName(organization.getOrgName());
                }

                userVoList.add(userVo);
            }
        }

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",((Page)userList).getTotal());
        dataMap.put("pages",((Page)userList).getPages());
        dataMap.put("pageNo",pageno);
        dataMap.put("pageSize",pagesize);
        dataMap.put("pageData",userVoList);*/
        dataMap.put("total",userVoPageInfo.getTotal());
        dataMap.put("rows",userVoPageInfo.getList());

        return dataMap;
    }

    public int add(UserVo userVo) throws Exception{
        TdUser user=new TdUser();
        PropertyUtils.copyProperties(user,userVo);
        user.setPassword(SecurityUtil.encryptMd5(SecurityUtil.encryptSHA("123456")));
        user.setCreateDate(new Date());
        user.setIsValid(true);
        userMapper.insert(user);

        TdUserRole userRole=new TdUserRole();
        userRole.setUserId(user.getUserId());
        userRole.setRoleId(userVo.getRoleId());
        userRole.setCreateDate(new Date());
        userRole.setIsValid(true);
        userRoleMapper.insert(userRole);

        return 1;
    }

    public int update(UserVo userVo) throws Exception{
        TdUser user=new TdUser();
        PropertyUtils.copyProperties(user,userVo);
        userMapper.updateByPrimaryKeySelective(user);

        if (userVo.getRoleId()!=null){
            TdUserRoleExample userRoleExample=new TdUserRoleExample();
            userRoleExample.createCriteria().andUserIdEqualTo(user.getUserId());
            List<TdUserRole> userRoleList=userRoleMapper.selectByExample(userRoleExample);
            if (userRoleList!=null&&userRoleList.size()>0){
                TdUserRole userRole=userRoleList.get(0);
                userRole.setRoleId(userVo.getRoleId());
                userRoleMapper.updateByPrimaryKeySelective(userRole);
            }
        }

        return 1;
    }

    public int updateUserInfo(Map<String, Object> map) throws Exception {
        userMapper.updateUserInfo(map);
        return 0;
    }

    //@Override
    public Map<String,Object> delete(Integer[] userIds){
        Map<String,Object> dataMap = new HashMap<String, Object>();

        for (Integer userId:userIds){
//            TdUser user=new TdUser();
//            user.setUserId(userId);
//            user.setIsValid(false);
            userRoleMapper.deleteByUserId(userId);
            userMapper.deleteByPrimaryKey(userId);
//
//            TdUserRoleExample userRoleExample=new TdUserRoleExample();
//            userRoleExample.createCriteria().andUserIdEqualTo(user.getUserId());
//            List<TdUserRole> userRoleList=userRoleMapper.selectByExample(userRoleExample);
//            if (userRoleList!=null&&userRoleList.size()>0){
//                TdUserRole userRole=userRoleList.get(0);
//                userRole.setIsValid(false);
//                userRoleMapper.updateByPrimaryKeySelective(userRole);
//            }

        }

        dataMap.put("result","删除成功。");
        return dataMap;
    }

    public void updateLoginTimeByLogCode(Map<String,Object> map) {
        userMapper.updateLoginTimeByLogCode(map);
    }
}
